--
-- Author: shunguo.chen
-- Date: 2016-06-20 12:38:27
--
-- ShareModel

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
	self.super.ctor(self, data)
end

function ClassRef:init()
	self._bossDatas = nil
	self._bossCompData = nil
	self._diffNum = 4

	self._mountData = nil
	self._mountMaxCnt = nil
	self._mountGotCnt = nil
end

function ClassRef:updateAchievement(uid, achieve)
	if achieve and achieve:isComp() then
		local conf = GD:getAchievementConfig(uid)
		if conf and conf.time == "1" then
			uid = tonumber(uid)
			self:getBossDatas()
			local diff
			for i, datas in ipairs(self._bossDatas) do
				local found = false
				for j, data in ipairs(datas) do
					if data.achId == uid then
						data.comp = true
						found = true
						break
					end
				end
				if found then
					diff = i
					break
				end
			end
			-- if diff then
			-- 	app:sendMsg("ShareController", "shareBoss", diff)
			-- end
		end
	end
end

function ClassRef:updateMount(uid, mount, opt)
	if mount and opt == "insert" then
		if self._mountData then
			self._mountGotCnt = self._mountGotCnt + 1
			self._mountData[mount.tid] = true
		else
			self:getMountData()
		end
		-- app:sendMsg("ShareController", "shareMount", mount.tid)
	end
end

function ClassRef:getDiffNum()
	self:getBossDatas()
	return self._diffNum
end

function ClassRef:getBossCompData()
	self:getBossDatas()
	return self._bossCompData
end

function ClassRef:getBossDatas(diff)
	if not self._bossDatas then
		local confs = GD:queryDungeon()
		self._bossDatas = {}
		self._bossCompData = {}
		
		for i,conf in ipairs(confs) do
			local achAry = string.splitTwice(conf.achID, nil, true)
			local diffAry = string.split(conf.difficulty, "|")
			if not self._diffNum then
				self._diffNum = #diffAry
			end
			for j,v in ipairs(diffAry) do
				self._bossDatas[j] = self._bossDatas[j] or {}
				self._bossDatas[j] = table.attach(self._bossDatas[j], achAry[j])
			end
		end

		for i,ary in ipairs(self._bossDatas) do
			local comNum = 0
			for j,v in ipairs(ary) do
				ary[j] = {achId=v, comp=UD:isAchievementComplete(v)}
				if ary[j].comp then
					comNum = comNum + 1
				end
			end
			self._bossCompData[i] = comNum
		end

		dump(self._bossDatas, "ShareModel._bossDatas")
		dump(self._bossCompData, "ShareModel._bossCompData")
	end
	return diff and self._bossDatas[diff] or self._bossDatas
end

function ClassRef:getMountCompData()
	self:getMountData()
	return self._mountGotCnt, self._mountMaxCnt
end

function ClassRef:getMountData()
	if not self._mountData then
		local confs = GD:getMountConfigs()
		self._mountData = {}

		self._mountMaxCnt = 0
		self._mountGotCnt = 0
		for k,v in pairs(confs) do
			self._mountMaxCnt = self._mountMaxCnt + 1
			local id = tonumber(v.ID)
			local mount = UD:getMountByID(id)
			if mount then
				self._mountGotCnt = self._mountGotCnt + 1
			end
			self._mountData[id] = (mount~= nil)
		end
	end
	return self._mountData
end

----------------------------------------- request ------------------------------------
-- ClassRef.shareType = -1
function ClassRef:share(shareType, con, path, type)
	-- self.shareType = self.shareType + 1
	-- shareType = self.shareType % 6 + 1

	local title = L("tips_3000163")
	local function sdkShareCallback(api, ret)
		print("ShareModel.share", tostring(ret), shareType)
		if ret == "success" then
			if shareType == Enums.ShareType.qqZone then
				display.pushToast(L("tips_3000295"))
			elseif shareType == Enums.ShareType.wxCircle then
				display.pushToast(L("tips_3000296"))
			end
			self:getShareReward(type)
		else
			
		end
	end
	app.channelAPI:share(sdkShareCallback, shareType, title, con, path)

	-- test shareToFriend
	-- local openId = "EC4012A6D5194480DB08ADE7DA31F671"
	-- local title = "后端分享"
	-- local desc = "这是一个后端分享，送你一个红心"
	-- local ext = "这是额外透传信息"
	-- app.channelAPI:shareToFriend(openId, title, desc, ext, function(api, ret)
	-- 	print("shareToFriend", ret)
	-- end)
end

function ClassRef:getShareReward(type)
	app.session:request("action.C2S_SocialShared", {type=type}, function(ret)
		dump(ret, "ShareModel.C2S_SocialShared")
		if ret.ok then
			
		end
	end)
end

return ClassRef